#include <interrupt.h>

#if (INT0_ENABLE_EN | INT1_ENABLE_EN | INT2_ENABLE_EN | INT3_ENABLE_EN | INT4_ENABLE_EN)

#if INT0_ENABLE_EN
static void (*func_intr0_cb)() = NULL;
static interrupt_modes intr0_intr_mode = FALLING;
#endif
#if INT1_ENABLE_EN
static void (*func_intr1_cb)() = NULL;
static interrupt_modes intr1_intr_mode = FALLING;
#endif
#if INT2_ENABLE_EN
static void (*func_intr2_cb)() = NULL;
#endif
#if INT3_ENABLE_EN
static void (*func_intr3_cb)() = NULL;
#endif
#if INT4_ENABLE_EN
static void (*func_intr4_cb)() = NULL;
#endif

void interrupt_enable(interrupt_t intr_t)
{
    switch (intr_t)
    {
#if INT0_ENABLE_EN
    case interrupt_0:
        if (func_intr0_cb == NULL)
            return;
        EX0 = 1; //使能INT0中断
        break;
#endif

#if INT1_ENABLE_EN
    case interrupt_1:
        if (func_intr1_cb == NULL)
            return;
        EX1 = 1; //使能INT0中断
        break;
#endif

#if INT2_ENABLE_EN
    case interrupt_2:
        if (func_intr2_cb == NULL)
            return;
        INTCLKO |= 0X10;
        break;
#endif

#if INT3_ENABLE_EN
    case interrupt_3:
        if (func_intr3_cb == NULL)
            return;
        INTCLKO |= 0X20;
        break;
#endif

#if INT4_ENABLE_EN
    case interrupt_4:
        if (func_intr4_cb == NULL)
            return;
        INTCLKO |= 0X40;
        break;
#endif

    default:
        break;
    }
    EA = 1;
}

void interrupt_disable(interrupt_t intr_t)
{
    switch (intr_t)
    {
#if INT0_ENABLE_EN
    case interrupt_0:
        EX0 = 0; //使能INT0中断
        break;
#endif

#if INT1_ENABLE_EN
    case interrupt_1:
        EX1 = 0; //使能INT0中断
        break;
#endif
#if INT2_ENABLE_EN
    case interrupt_2:
        INTCLKO &= ~0X10;
        break;
#endif

#if INT3_ENABLE_EN
    case interrupt_3:
        INTCLKO &= ~0X20;
        break;
#endif

#if INT4_ENABLE_EN
    case interrupt_4:
        INTCLKO &= ~0X40;
        break;
#endif
    default:
        break;
    }
}

void interrupt_config(interrupt_t intr_t, void (*func)(), interrupt_modes mode)
{
    switch (intr_t)
    {
#if INT0_ENABLE_EN
    case interrupt_0:
        intr0_intr_mode = mode;
        if (mode == FALLING)
            IT0 = 1; //使能INT0下降沿中断
        else
            IT0 = 0;
        func_intr0_cb = func;
        break;
#endif
#if INT1_ENABLE_EN
    case interrupt_1:
        intr0_intr_mode = mode;
        if (mode == FALLING)
            IT1 = 1; //使能INT0下降沿中断
        else
            IT0 = 0;
        func_intr1_cb = func;
        break;
#endif
#if INT2_ENABLE_EN
    case interrupt_2:
        if (mode == RISING)
            return;
        func_intr2_cb = func;
        break;
#endif
#if INT3_ENABLE_EN
    case interrupt_3:
        if (mode == RISING)
            return;
        func_intr3_cb = func;
        break;
#endif
#if INT4_ENABLE_EN
    case interrupt_4:
        if (mode == RISING)
            return;
        func_intr4_cb = func;
        break;
#endif

    default:
        break;
    }
}

#if INT0_ENABLE_EN

void INT0_Isr() interrupt 0
{
    if (intr0_intr_mode != RISING)
    {
        func_intr0_cb();
    }
    else if (P32)
    {
        func_intr0_cb();
    }
}

#endif

#if INT1_ENABLE_EN
void INT1_Isr() interrupt 2
{
    if (intr1_intr_mode != RISING)
    {
        func_intr1_cb();
    }
    else if (P33)
    {
        func_intr1_cb();
    }
}
#endif
#if INT2_ENABLE_EN
void INT2_Isr() interrupt 10
{
    func_intr2_cb();
}
#endif
#if INT3_ENABLE_EN
void INT3_Isr() interrupt 11
{

    func_intr3_cb();
}
#endif

#if INT4_ENABLE_EN
void INT4_Isr() interrupt 16
{
    func_intr4_cb();
}
#endif

#endif
